package com.ruoyi.campadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.campadmin.mapper.CaVenueRentalMapper;
import com.ruoyi.campadmin.service.ICaVenueRentalService;
import com.ruoyi.campadmin.domain.CaVenueRental;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.campadmin.domain.CaCamp;
import com.ruoyi.campadmin.service.ICaCampService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CaVenueRentalServiceImpl implements ICaVenueRentalService {


    @Resource
    private CaVenueRentalMapper caVenueRentalMapper;
    
    
    @Resource
    private ICaCampService caCampService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param caVenueRental 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<CaVenueRental> getCaVenueRentalPage(PageDomain pageDomain, CaVenueRental caVenueRental , CaCamp caCamp) {
        if (Objects.isNull(caVenueRental)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<CaVenueRental> queryWrapper = new LambdaQueryWrapper<>(new CaVenueRental());
            //if(!Objects.isNull(caVenueRental.getName())){
            //	queryWrapper.like(CaVenueRental::getName,caVenueRental.getName());
            //}
            

            if (!Objects.isNull(caVenueRental.getId())) {
                queryWrapper.eq(CaVenueRental::getId, caVenueRental.getId());
            }

            if (!Objects.isNull(caVenueRental.getCaCampId())) {
                queryWrapper.eq(CaVenueRental::getCaCampId, caVenueRental.getCaCampId());
            }

            caCamp.setBaseEntityNull();
            caCamp.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(caCamp)) {
                List<Long> idList = caCampService.getIdListByCaCamp(caCamp);
                queryWrapper.in(CaVenueRental::getCaCampId, idList);
            }


            if (!Objects.isNull(caVenueRental.getFreight())) {
                queryWrapper.like(CaVenueRental::getFreight, caVenueRental.getFreight());
            }

            if (!Objects.isNull(caVenueRental.getService())) {
                queryWrapper.like(CaVenueRental::getService, caVenueRental.getService());
            }

            if (!Objects.isNull(caVenueRental.getTitle())) {
                queryWrapper.like(CaVenueRental::getTitle, caVenueRental.getTitle());
            }

            if (!Objects.isNull(caVenueRental.getLostPrice())) {
                queryWrapper.like(CaVenueRental::getLostPrice, caVenueRental.getLostPrice());
            }

            if (!Objects.isNull(caVenueRental.getDetailText())) {
                queryWrapper.like(CaVenueRental::getDetailText, caVenueRental.getDetailText());
            }

            if (!Objects.isNull(caVenueRental.getPhotoUrl())) {
                queryWrapper.like(CaVenueRental::getPhotoUrl, caVenueRental.getPhotoUrl());
            }

            if (!Objects.isNull(caVenueRental.getPraiseNum())) {
                queryWrapper.like(CaVenueRental::getPraiseNum, caVenueRental.getPraiseNum());
            }

            if (!Objects.isNull(caVenueRental.getCollectionNum())) {
                queryWrapper.like(CaVenueRental::getCollectionNum, caVenueRental.getCollectionNum());
            }

            if (!Objects.isNull(caVenueRental.getStatus())) {
                queryWrapper.like(CaVenueRental::getStatus, caVenueRental.getStatus());
            }

            if (!Objects.isNull(caVenueRental.getDelFlag())) {
                queryWrapper.like(CaVenueRental::getDelFlag, caVenueRental.getDelFlag());
            }

            if (!Objects.isNull(caVenueRental.getCreateBy())) {
                queryWrapper.like(CaVenueRental::getCreateBy, caVenueRental.getCreateBy());
            }

            if (!Objects.isNull(caVenueRental.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caVenueRental.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caVenueRental.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaVenueRental::getCreateTime, caVenueRental.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caVenueRental.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaVenueRental::getCreateTime, caVenueRental.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caVenueRental.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caVenueRental.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaVenueRental::getCreateTime, caVenueRental.getParams().get("CreateTimeBeginTime"), caVenueRental.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caVenueRental.getUpdateBy())) {
                queryWrapper.like(CaVenueRental::getUpdateBy, caVenueRental.getUpdateBy());
            }

            if (!Objects.isNull(caVenueRental.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caVenueRental.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caVenueRental.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaVenueRental::getUpdateTime, caVenueRental.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caVenueRental.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaVenueRental::getUpdateTime, caVenueRental.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caVenueRental.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caVenueRental.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaVenueRental::getUpdateTime, caVenueRental.getParams().get("UpdateTimeBeginTime"), caVenueRental.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caVenueRental.getRemark())) {
                queryWrapper.like(CaVenueRental::getRemark, caVenueRental.getRemark());
            }
        Page<CaVenueRental> caVenueRentalList= caVenueRentalMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(CaVenueRental record:caVenueRentalList.getRecords()){
            record.setCaCamp(caCampService.getCaCampById(record.getCaCampId()));
        }

            return caVenueRentalList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public CaVenueRental getCaVenueRentalById(Long id) {
        if (Objects.isNull(id)) {
            return new CaVenueRental();
        }
        try {
            CaVenueRental caVenueRental = caVenueRentalMapper.selectById(id);
            caVenueRental.setCaCamp(caCampService.getCaCampById(caVenueRental.getCaCampId()));
            if (Objects.isNull(caVenueRental)){
                caVenueRental = new CaVenueRental();
            }
            return caVenueRental;

        } catch (Exception e) {
            e.printStackTrace();
            return new CaVenueRental();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param caVenueRental 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByCaVenueRental(CaVenueRental caVenueRental) {
        if (Objects.isNull(caVenueRental)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<CaVenueRental> queryWrapper = new LambdaQueryWrapper<>(new CaVenueRental());
            

            if (!Objects.isNull(caVenueRental.getId())) {
                queryWrapper.eq(CaVenueRental::getId, caVenueRental.getId());
            }

            if (!Objects.isNull(caVenueRental.getCaCampId())) {
                queryWrapper.eq(CaVenueRental::getCaCampId, caVenueRental.getCaCampId());
            }

            if (!Objects.isNull(caVenueRental.getFreight())) {
                queryWrapper.like(CaVenueRental::getFreight, caVenueRental.getFreight());
            }

            if (!Objects.isNull(caVenueRental.getService())) {
                queryWrapper.like(CaVenueRental::getService, caVenueRental.getService());
            }

            if (!Objects.isNull(caVenueRental.getTitle())) {
                queryWrapper.like(CaVenueRental::getTitle, caVenueRental.getTitle());
            }

            if (!Objects.isNull(caVenueRental.getLostPrice())) {
                queryWrapper.like(CaVenueRental::getLostPrice, caVenueRental.getLostPrice());
            }

            if (!Objects.isNull(caVenueRental.getDetailText())) {
                queryWrapper.like(CaVenueRental::getDetailText, caVenueRental.getDetailText());
            }

            if (!Objects.isNull(caVenueRental.getPhotoUrl())) {
                queryWrapper.like(CaVenueRental::getPhotoUrl, caVenueRental.getPhotoUrl());
            }

            if (!Objects.isNull(caVenueRental.getPraiseNum())) {
                queryWrapper.like(CaVenueRental::getPraiseNum, caVenueRental.getPraiseNum());
            }

            if (!Objects.isNull(caVenueRental.getCollectionNum())) {
                queryWrapper.like(CaVenueRental::getCollectionNum, caVenueRental.getCollectionNum());
            }

            if (!Objects.isNull(caVenueRental.getStatus())) {
                queryWrapper.like(CaVenueRental::getStatus, caVenueRental.getStatus());
            }

            if (!Objects.isNull(caVenueRental.getDelFlag())) {
                queryWrapper.like(CaVenueRental::getDelFlag, caVenueRental.getDelFlag());
            }

            if (!Objects.isNull(caVenueRental.getCreateBy())) {
                queryWrapper.like(CaVenueRental::getCreateBy, caVenueRental.getCreateBy());
            }

            if (!Objects.isNull(caVenueRental.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caVenueRental.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caVenueRental.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaVenueRental::getCreateTime, caVenueRental.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caVenueRental.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaVenueRental::getCreateTime, caVenueRental.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caVenueRental.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caVenueRental.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaVenueRental::getCreateTime, caVenueRental.getParams().get("CreateTimeBeginTime"), caVenueRental.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caVenueRental.getUpdateBy())) {
                queryWrapper.like(CaVenueRental::getUpdateBy, caVenueRental.getUpdateBy());
            }

            if (!Objects.isNull(caVenueRental.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caVenueRental.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caVenueRental.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaVenueRental::getUpdateTime, caVenueRental.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caVenueRental.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaVenueRental::getUpdateTime, caVenueRental.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caVenueRental.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caVenueRental.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaVenueRental::getUpdateTime, caVenueRental.getParams().get("UpdateTimeBeginTime"), caVenueRental.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caVenueRental.getRemark())) {
                queryWrapper.like(CaVenueRental::getRemark, caVenueRental.getRemark());
            }

            List<CaVenueRental> caVenueRentals = caVenueRentalMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaVenueRental caVenueRentalNo:caVenueRentals){
                idList.add(caVenueRentalNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过caCamp外键idList查询idList
     *
     * @param caCampIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByCaCampIdList(List<Long> caCampIdList) {
        if (Objects.isNull(caCampIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<CaVenueRental> queryWrapper = new LambdaQueryWrapper<>(new CaVenueRental());
            queryWrapper.in(CaVenueRental::getCaCampId, caCampIdList);
            List<CaVenueRental> caVenueRentals = caVenueRentalMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaVenueRental caVenueRental:caVenueRentals){
                idList.add(caVenueRental.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param caVenueRental 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertCaVenueRental(CaVenueRental caVenueRental) {
        if (Objects.isNull(caVenueRental)) {
            return -1;
        }
        try {
        
            //caVenueRental.setSpell(PinYin.getPinYin(caVenueRental.getName()));
            return caVenueRentalMapper.insert(caVenueRental);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param caVenueRental 实体类
     * @return 结果
     */
    @Override
    public int updateCaVenueRentalById(CaVenueRental caVenueRental) {
        if (Objects.isNull(caVenueRental)) {
            return -1;
        }
        try {
            //caVenueRental.setSpell(PinYin.getPinYin(caVenueRental.getName()));
            return caVenueRentalMapper.updateById(caVenueRental);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteCaVenueRentalByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return caVenueRentalMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<CaVenueRental> getCaVenueRentalList() {
        try {
            LambdaQueryWrapper<CaVenueRental> queryWrapper = new LambdaQueryWrapper<>(new CaVenueRental());
            queryWrapper.isNotNull(CaVenueRental::getId);
            return caVenueRentalMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
